#ifndef DAILYWIDGET_H
#define DAILYWIDGET_H

#include <QGraphicsView>
#include <QDateTime>
#include <QSqlDatabase>
#include <QMenu>
#include "calendaritem.h"
#include "graphicstextitemsized.h"

class DailyWidget : public QGraphicsView
{
    Q_OBJECT
public:
    explicit DailyWidget(QWidget *parent = 0);

    void resizeEvent(QResizeEvent*);
    void mouseMoveEvent(QMouseEvent* e);
    void mousePressEvent(QMouseEvent* e);
    void mouseReleaseEvent(QMouseEvent* e);
    void mouseDoubleClickEvent(QMouseEvent* e);
    void lbuttonDown(QMouseEvent* e);
    void rbuttonDown(QMouseEvent* e);

    const QSqlDatabase& db() const{ return db_; }
    void setDatabase(const QSqlDatabase& db){ db_ = db; }

    int userID() const{ return userID_; }
    void setUserID(int userid){ userID_ = userid; emit userIDChanged(userID_); }

    const QDate& startDate() const{ return startDate_; }
    const QDate& endDate() const{ return endDate_; }
    void setDate(const QDate& date){ startDate_ = date; endDate_ = date; emit dateChanged(startDate_, endDate_); }
    void setDate(const QDate& startDate, const QDate& endDate){ startDate_ = startDate; endDate_ = endDate; emit dateChanged(startDate_, endDate_); }

    const QString& dateFormat() const{ return dateFormat_; }
    void setDateFormat(const QString& format){ dateFormat_ = format; emit dateFormatChanged(format); }

    bool showTimeTable() const{ return showTimeTable_; }
    void setShowTimeTable(bool show){ showTimeTable_ = show; emit timeTableVisibled(show); }

    qreal heightOfHour() const{ return heightOfHour_; }
    void setHeightOfHour(qreal height){ heightOfHour_ = height; emit heightOfHourChanged(height); }

    const QRectF& calendarRect() const{ return calendarRect_; }
    const QRectF& timeRect() const{ return timeRect_; }
    const QList<QRectF>& taskRectList() const{ return taskRectList_; }

private slots:
    void on_userID_changed(int userID);
    void on_timeTable_visibled(bool visible);
    void on_date_changed(const QDate& startDate, const QDate& endDate);
    void on_dateFormat_changed(const QString& format);
    void on_heightOfHour_changed(qreal height);
    void on_taskEdit_triggered();
    void on_taskDelete_triggered();
    void on_vscrollbar_valueChanged(int value);
    void on_sceneRectChanged(const QRectF&);

signals:
    void userIDChanged(int userID);
    void timeTableVisibled(bool visible);
    void dateChanged(const QDate& startDate, const QDate& endDate);
    void dateFormatChanged(const QString& format);
    void heightOfHourChanged(qreal height);

private:
    void createGraphicItems();
    void createTimeLTable();
    void createDateTable();
    void createTaskList();
    void createTask(const CalendarItem& item);
    bool getTime(const QPointF& p, QDateTime& dateTime);
    QList<QRectF> getTaskRectList(const QDateTime& time1, const QDateTime& time2);
    void updateItemList();
    bool getTaskItems(int x, int y, int* taskID, QList<QGraphicsItem*>* taskList);
    void previewTask(QDateTime time1, QDateTime time2, const QColor& color, CalendarItem* item=NULL);
    bool copyTask(int taskID, int& newID);
    void editTask(int taskID);
    void deleteTask(int taskID, bool confirm);
    void updateDatabase(const CalendarItem& item);

private:
    QSqlDatabase db_;
    int userID_;
    QDate startDate_;
    QDate endDate_;
    QString dateFormat_;
    bool showTimeTable_;
    qreal heightOfHour_;
    qreal timeInterval_;

    QSize clientSize_;
    QRectF calendarRect_;
    QRectF dateRect_;
    QRectF timeRect_;
    QList<QRectF> taskRectList_;
    QGraphicsRectItem* dateBackground_;
    QList<QGraphicsSimpleTextItem*> dateTextList_;

    QMenu taskPopupMenu;

    // task list
    CalendarItems taskList_;

    // mouse event
    Qt::KeyboardModifiers modifiers;
    QPoint mousePressedPosition;

    // time objects for creating or editing task
    QDateTime time1, time2;
    QGraphicsSimpleTextItem* taskTime;

    // select task
    int selectingTaskID;
    QList<QGraphicsItem*> selectingItemList;
    QColor selectingColor;
    int movingTaskID;
    int resizingTaskID;
    int copyingTaskID;

    // creating task
    QList<QGraphicsItem*> creatingTaskList;
};

#endif // DAILYWIDGET_H
